/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.analysisModel;

import java.io.Serializable;
import java.util.ArrayList;

import br.ufmg.dees.insane.util.IMatrix;
import br.ufmg.dees.insane.util.IVector;

/** A class representing PlaneStress
 * @author Jamile Fuina
 * @since 06/05/2005
 */
public class PlaneStrain extends Plane implements Serializable{
	
	private static final long serialVersionUID = 1L;

	/**Constructs a Plane Strain Analysis Model.*/
    public PlaneStrain()
	{
    	super();
	}
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getLabel()
     */
    public String getLabel() {
        return "PlaneStrain";
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getDualInternalVariableOperator(br.ufmg.dees.insane.util.IMatrix)
     */
    public IMatrix getDualInternalVariableOperator(IMatrix e){
    	IMatrix d = new IMatrix(3,3);
		d.setZero();
		double elast = e.getElement(0,0);
		double ni = e.getElement(2,0);
		double k = ((elast)/(1+ni)*(1-2*ni));
		d.setElement(0,0,(1-ni)*k);
		d.setElement(0,1,(ni*k));
		d.setElement(0,2,0);
		d.setElement(1,0,(ni*k));
		d.setElement(1,1,(1-ni)*k);
		d.setElement(1,2,0);
		d.setElement(2,0,0);
		d.setElement(2,1,0);
		d.setElement(2,2,(((1-2*ni)/2)*k));
		return(d);
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getInternalVariablesTensor(br.ufmg.dees.insane.util.IVector, br.ufmg.dees.insane.util.IMatrix)
     */
    public IMatrix getInternalVariablesTensor(IVector a, IMatrix b){
    	return expandTensor(a); 	
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getInternalDualVariablesTensor(br.ufmg.dees.insane.util.IVector, br.ufmg.dees.insane.util.IMatrix)
     */
    public IMatrix getDualInternalVariablesTensor(IVector a, IMatrix b){
        IMatrix Tensor = new IMatrix(3,3);
        Tensor.setElement(0,0,a.getElement(0));
        Tensor.setElement(0,1,a.getElement(2));
        Tensor.setElement(1,1,a.getElement(1));
        Tensor.setElement(1,0,a.getElement(2));
        Tensor.setElement(2,2,b.getElement(2,0)*(a.getElement(0)+a.getElement(1)));
        return Tensor;   
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#expandVector(br.ufmg.dees.insane.util.IVector)
     */
    public IVector expandVector(IVector a){
    	IVector Vector = new IVector(6);
        Vector.setElement(0,a.getElement(0));
        Vector.setElement(1,a.getElement(1));
        Vector.setElement(3,a.getElement(2));
        return Vector;
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#expandTensor(br.ufmg.dees.insane.util.IVector)
     */
    public IMatrix expandTensor(IVector a){
    	IMatrix Tensor = new IMatrix(3,3);
        Tensor.setElement(0,0,a.getElement(0));
        Tensor.setElement(0,1,a.getElement(2));
        Tensor.setElement(1,1,a.getElement(1));
        Tensor.setElement(1,0,a.getElement(2));
        return Tensor;
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#reduceVector(br.ufmg.dees.insane.util.IVector)
     */
    public IVector reduceVector(IVector a){
    	IVector Vector = new IVector(3);
        Vector.setElement(0,a.getElement(0));
        Vector.setElement(1,a.getElement(1));
        Vector.setElement(2,a.getElement(3));
        return Vector;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#reduceTensor(br.ufmg.dees.insane.util.IMatrix)
     */
    public IVector reduceTensor(IMatrix a){
    	IVector Vector = new IVector(3);
        Vector.setElement(0,a.getElement(0,0));
        Vector.setElement(1,a.getElement(1,1));
        Vector.setElement(2,a.getElement(0,1));
        return Vector;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getDeviatoricDualInternalVariable(br.ufmg.dees.insane.util.IMatrix)
     */
    public IVector getDeviatoricDualInternalVariable(IVector s){
        IVector deviatoric = new IVector(4);            // cuidado, aqui o parametro � o vetor de tensoes completo
        double p = (s.getElement(0) + s.getElement(1) + s.getElement(2))/3.0;
        deviatoric.setElement(0,s.getElement(0)- p);
        deviatoric.setElement(1,s.getElement(1)- p);
        deviatoric.setElement(2,s.getElement(2)- p);
        deviatoric.setElement(3,s.getElement(3));
        return deviatoric;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getDeviatoricInternalVariable(br.ufmg.dees.insane.util.IMatrix)
     */
    public IVector getDeviatoricInternalVariable(IVector e){	
        IVector deviatoric = new IVector(4);
        double p = (e.getElement(0) + e.getElement(1))/3.0;
        deviatoric.setElement(0,e.getElement(0)- p);
        deviatoric.setElement(1,e.getElement(1)- p);
        deviatoric.setElement(2,-p);
        deviatoric.setElement(3,e.getElement(2));
        return deviatoric;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getJ2(br.ufmg.dees.insane.util.IMatrix)
     */
    public double getJ2(IVector s){
    	IVector dev = this.getDeviatoricDualInternalVariable(s);
        double J2  = (dev.getElement(0)* dev.getElement(0)+dev.getElement(1)* dev.getElement(1)+dev.getElement(2)* dev.getElement(2))/2.0 + dev.getElement(3)* dev.getElement(3);
        return(J2);
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getdJ2dSIG(br.ufmg.dees.insane.util.IMatrix)
     */
    public IVector getdJ2dSIG (IVector s){
    	IVector derivate = new IVector(3);
        derivate.setElement(0,(2.0*s.getElement(0)-s.getElement(1)-s.getElement(2))/3.0);
        derivate.setElement(1,(2.0*s.getElement(1)-s.getElement(0)-s.getElement(2))/3.0);
        derivate.setElement(2,(2.0*s.getElement(2)-s.getElement(0)-s.getElement(1))/3.0);
        derivate.setElement(3,(2.0*s.getElement(3)));
        return derivate;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getJ3(br.ufmg.dees.insane.util.IMatrix)
     */
    public double getJ3(IVector s){
    	IVector dev = this.getDeviatoricDualInternalVariable(s);
        double J3  = dev.getElement(0)* dev.getElement(1)* dev.getElement(2) - dev.getElement(3)* dev.getElement(3)* dev.getElement(2);
        return(J3);
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getdJ3dSIG(br.ufmg.dees.insane.util.IMatrix)
     */
    public IVector getdJ3dSIG (IVector s){
    	IVector derivate = new IVector(4);
    	double dJ3dsx = ((1.0/9.0) * (4.0*s.getElement(1)*s.getElement(2)-2.0*s.getElement(0)*s.getElement(2)-s.getElement(2)*s.getElement(2)-2.0*s.getElement(0)*s.getElement(1)-s.getElement(1)*s.getElement(1)+2.0*s.getElement(0)*s.getElement(0)))+ ((1.0/3.0) * (s.getElement(3)*s.getElement(3)));
    	double dJ3dsy = ((1.0/9.0) * (4.0*s.getElement(0)*s.getElement(2)-2.0*s.getElement(1)*s.getElement(2)-s.getElement(2)*s.getElement(2)-2.0*s.getElement(0)*s.getElement(1)-s.getElement(0)*s.getElement(0)+2.0*s.getElement(1)*s.getElement(1)))+ ((1.0/3.0) * (s.getElement(3)*s.getElement(3)));
    	double dJ3dsz = ((1.0/9.0) * (4.0*s.getElement(0)*s.getElement(1)-2.0*s.getElement(0)*s.getElement(2)-s.getElement(0)*s.getElement(0)-2.0*s.getElement(2)*s.getElement(1)-s.getElement(1)*s.getElement(1)+2.0*s.getElement(2)*s.getElement(2)))- ((1.0/3.0) * (2.0*s.getElement(3)*s.getElement(3)));
        double dJ3dsxy = - ((1.0/3.0) * (4.0*s.getElement(3)*s.getElement(2)-2.0*s.getElement(3)*s.getElement(0)-2.0*s.getElement(3)*s.getElement(1)));
        derivate.setElement(0,dJ3dsx);
        derivate.setElement(1,dJ3dsy);
        derivate.setElement(2,dJ3dsz);
        derivate.setElement(3,dJ3dsxy);
        return derivate;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getI1(br.ufmg.dees.insane.util.IMatrix)
     */
    public double getI1(IVector s){
    	return(s.getElement(0) + s.getElement(1)+ s.getElement(2));
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getdI1dSIG(br.ufmg.dees.insane.util.IMatrix)
     */
    public IVector getdI1dSIG (IVector s){
    	IVector derivate = new IVector(3);
        derivate.setElement(0,1.0);
        derivate.setElement(1,1.0);
        derivate.setElement(2,1.0);
        return derivate;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getI2(br.ufmg.dees.insane.util.IMatrix)
     */
    public double getI2(IVector s){
    	return(s.getElement(0) * s.getElement(1)+ s.getElement(0) * s.getElement(2)+ s.getElement(1) * s.getElement(2) - s.getElement(3) * s.getElement(3));
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getdI2dSIG(br.ufmg.dees.insane.util.IMatrix)
     */
    public IVector getdI2dSIG (IVector s){
    	IVector derivate = new IVector(4);
        derivate.setElement(0, s.getElement(1) + s.getElement(2));
        derivate.setElement(1, s.getElement(0) + s.getElement(2));
        derivate.setElement(2, s.getElement(0) + s.getElement(1));
        derivate.setElement(3, -2.0*s.getElement(3));
        return derivate;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getI3(br.ufmg.dees.insane.util.IMatrix)
     */
    public double getI3(IVector s){
    	return (s.getElement(0) * s.getElement(1)* s.getElement(2)- s.getElement(2) * s.getElement(3)* s.getElement(3));
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#getdI3dSIG(br.ufmg.dees.insane.util.IMatrix)
     */
    public IVector getdI3dSIG (IVector s){
        IVector derivate = new IVector(4);
        derivate.setElement(0, s.getElement(1) * s.getElement(2));
        derivate.setElement(1, s.getElement(0) * s.getElement(2));
        derivate.setElement(2, s.getElement(0) * s.getElement(1)- s.getElement(3)* s.getElement(3));
        derivate.setElement(3, -2.0*s.getElement(2)*s.getElement(3));
        return derivate;
    }

	/* (non-Javadoc)
	 * @see br.ufmg.dees.insane.analysisModel.AnalysisModel#modifyInternalVariableOperator(br.ufmg.dees.insane.util.IMatrix, br.ufmg.dees.insane.util.IMatrix)
	 */
	public IMatrix modifyInternalVariableOperator(IMatrix e, IMatrix p) {
		return e;
	}

	public ArrayList getDOFLabels() {
		ArrayList <String> al = new ArrayList<String>();
		al.add("Dx");
		al.add("Dy");
		return al;
	}

	public IMatrix getPMatrix() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
